package com.cuckoo.cuckoo_app2.business.Imp;

import com.cuckoo.cuckoo_app2.MyApplication;
import com.cuckoo.cuckoo_app2.business.PlatformBusiness;
import com.cuckoo.cuckoo_app2.dao.Platform;
import com.cuckoo.cuckoo_app2.dao.UserPlatformDao;
import com.cuckoo.cuckoo_app2.greendao.DaoSession;
import com.cuckoo.cuckoo_app2.greendao.PlatformDao;

import java.util.ArrayList;
import java.util.List;

public class PlatformBusinessImp implements PlatformBusiness {

    @Override
    public Long insert(Platform platform) {
        DaoSession db = MyApplication.getInstances().getDaoSession();
        Long id = db.getPlatformDao().insert(platform);
        db.clear();
        return id;
    }

    @Override
    public void insertList(List<Platform> list) {
        DaoSession db = MyApplication.getInstances().getDaoSession();
        db.getPlatformDao().insertInTx(list);
        db.clear();
    }

    @Override
    public List<Platform> getAll() {
        // 获取数据库会话对象
        DaoSession db = MyApplication.getInstances().getDaoSession();
        List<Platform> list = db.getPlatformDao().queryBuilder()
                .orderDesc(PlatformDao.Properties.IsLogin)
                .orderDesc(PlatformDao.Properties.Start)
                .list();
        return list;
    }

    @Override
    public List<Platform> getWhereAll() {
        // 获取应用程序的数据库会话
        // 获取数据库会话对象
        DaoSession db = MyApplication.getInstances().getDaoSession();

        // 构建查询条件，查询所有IsLogin和Start属性均为1的Platform对象
        List<Platform> list = db.getPlatformDao().queryBuilder()
                .where(PlatformDao.Properties.IsLogin.eq(1))
                .where(PlatformDao.Properties.Start.eq(1))
                .where(PlatformDao.Properties.HasTips.notEq(1))
                .list();
        return list;
    }

    @Override
    public Platform getById(String id) {
        DaoSession db = MyApplication.getInstances().getDaoSession();
        List<Platform> list = db.getPlatformDao().queryBuilder().where(PlatformDao.Properties.Id.eq(id)).limit(1).list();
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Boolean updata(Platform platform) {
        DaoSession db = MyApplication.getInstances().getDaoSession();
        db.getPlatformDao().update(platform);
        return true;
    }

    @Override
    public Long count() {
        DaoSession db = MyApplication.getInstances().getDaoSession();
        return db.getPlatformDao().queryBuilder().count();
    }

    @Override
    public boolean delete(String id) {
        DaoSession db = MyApplication.getInstances().getDaoSession();
        Platform platform = db.getPlatformDao().queryBuilder().where(PlatformDao.Properties.Id.eq(id)).unique();
        if (platform != null) {
            db.getPlatformDao().delete(platform);
        }
        return true;
    }

    @Override
    public void syncList(List<UserPlatformDao> list) {
        DaoSession db = MyApplication.getInstances().getDaoSession();

        List<Platform> platformList = new ArrayList<>();
        Platform platform;
        for (UserPlatformDao userPlatformDao : list) {
            platform = new Platform();
            platform.setId(userPlatformDao.getPlatformId());
            platform.setTitle(userPlatformDao.getTitle());
            platform.setImageUrl(userPlatformDao.getImageUrl());
            platform.setIsLogin(userPlatformDao.getIsLogin());
            platform.setAccount(userPlatformDao.getAccount());
            platform.setStart(userPlatformDao.getStart());
            if(!userPlatformDao.getHasTips().isEmpty()){
                platform.setHasTips(Integer.parseInt(userPlatformDao.getHasTips()));
            }
            platform.setMessag(userPlatformDao.getMessag());
            platform.setToken(userPlatformDao.getToken());
            platform.setUserName(userPlatformDao.getUserName());
            platform.setUserPassord(userPlatformDao.getUserPassord());
            platform.setRetryCount(userPlatformDao.getRetryCount());
            platform.setParams(userPlatformDao.getParams());
            if(userPlatformDao.getTaskTime() != null){
                platform.setTaskTime(Long.valueOf(userPlatformDao.getTaskTime()));
            }
            platform.setCookie(userPlatformDao.getCookie());
            platform.setLocalStorage(userPlatformDao.getLocalStorage());
            platformList.add(platform);
        }

        // 先清除
        db.getPlatformDao().deleteAll();

        // 再插入
        db.getPlatformDao().insertInTx(platformList);

        db.clear();
    }
}
